﻿using Basic.FlowDAL;
using Basic.FlowModel.DB;
using Basic.FlowRemoteModel;
using Basic.FlowRemoteModel.FlowApp.Model;
using WeDonekRpc.Helper;
using WeDonekRpc.Model;

namespace Basic.FlowCollect.lmpl
{
    internal class FlowAppCollect : IFlowAppCollect
    {
        private readonly IFlowAppDAL _BasicDAL;

        public FlowAppCollect ( IFlowAppDAL basicDAL )
        {
            this._BasicDAL = basicDAL;
        }
        public DBFlowApp[] Query ( FlowAppQuery query, IBasicPage paging, out int count )
        {
            return this._BasicDAL.Query(query, paging, out count);
        }
        public DBFlowApp Get ( long id )
        {
            return this._BasicDAL.Get(id);
        }
        public T Get<T> ( long appId ) where T : class, new()
        {
            return this._BasicDAL.Get<T>(appId);
        }
        public bool SetSettleFormId ( DBFlowApp app, long? formId )
        {
            if ( app.SettleFormId == formId )
            {
                return false;
            }
            this._BasicDAL.SetSettleFormId(app, formId);
            return true;
        }
        public bool Set ( DBFlowApp app, FlowAppSet datum )
        {
            if ( app.Status == FlowAppStatus.启用 )
            {
                throw new ErrorException("flow.app.status.error");
            }
            else if ( app.AppName != datum.AppName && this._BasicDAL.IsExists(c => c.AppName == datum.AppName) )
            {
                throw new ErrorException("flow.app.name.exists");
            }
            return this._BasicDAL.Update(app, datum);
        }
        public long Add ( FlowAppSet data )
        {
            if ( this._BasicDAL.IsExists(c => c.AppName == data.AppName) )
            {
                throw new ErrorException("flow.app.name.exists");
            }
            return this._BasicDAL.Add(data);
        }
        public bool Enable ( DBFlowApp app )
        {
            if ( app.Status == FlowAppStatus.启用 )
            {
                return false;
            }
            else if ( !app.FlowId.HasValue )
            {
                throw new ErrorException("flow.app.flow.null");
            }
            this._BasicDAL.Enable(app);
            return true;
        }
        public bool Stop ( DBFlowApp app )
        {
            if ( app.Status == FlowAppStatus.停用 )
            {
                return false;
            }
            this._BasicDAL.Stop(app);
            return true;
        }

        public bool SetFlow ( DBFlowApp app, AppFlowSet set )
        {
            if ( app.Status == FlowAppStatus.启用 )
            {
                throw new ErrorException("flow.app.status.error");
            }
            return this._BasicDAL.Update(app, set);
        }

        public string GetAppName ( long appId )
        {
            return this._BasicDAL.Get(a => a.Id == appId, a => a.AppName);
        }

        public Result[] Gets<Result> ( FlowAppType? appType, FlowAppStatus status ) where Result : class, new()
        {
            if ( appType.HasValue )
            {
                return this._BasicDAL.Gets<Result>(a => a.AppType == appType.Value && a.Status == status);
            }
            return this._BasicDAL.Gets<Result>(a => a.Status == status);
        }

        public void Delete ( DBFlowApp app )
        {
            if ( app.Status != FlowAppStatus.起草 )
            {
                throw new ErrorException("flow.app.status.error");
            }
            this._BasicDAL.Delete(app.Id);
        }
    }
}
